const superagent = require("superagent")
var charset = require('superagent-charset');
const cheerio = require('cheerio');
const axios = require('axios')
charset(superagent); //设置字符

var BASE_URL='http://www.zj12580.cn/dept/2449';
var LAST='';
var timer='';
var docArr=[
    '[马景]副主任中医师',
    // '[徐新亚]副主任中医师',
];
// callbacks
function getYuYuelink(count){
    if(count>1) clearTimeout(timer);
    if(count==3000000) {
        console.log('查询结束')
        return;
    }
    superagent
    .get(BASE_URL)
    .charset('utf-8')
    .buffer(true)
    .end((error, response) => {
        //console.log(response.text)
        //结合 cheerio
      var dom = (response||{}).text;
      if(dom){
        const $ = cheerio.load(dom,{ decodeEntities: false });
        $('.right_box_1_r tr').each(function(index,item){
            
            var text= $(item).find('td').eq(0).text().trim();
            text=text.replace(/\s+/g,"");
            if(docArr[0]===text){
                //btnyy yy cz
                var res=[];
                $(item).find('td').each(function(index,s){
                    if($(s).find('form').length>0){
                            var status =$(s).find("input[name='docName']").val()+$(s).find("input[name='orderDate']").val()+($(s).find("input[type='submit']").val() ||$(s).text());
                            status=status && status.replace(/\s+/g,"");
                            res.push(status);
                    }
                })
                var resStr=res.join('\n');
                if(resStr!=LAST){
                    LAST=resStr;
                    console.log(LAST)
                    axios.get('http://wxmsg.dingliqc.com/demo/send/custom/UID_LET5BYBwfyMHAOizxrH4gkkhAvhv?content='+encodeURIComponent(LAST))
                    .then((response) => {
                        console.log('success')
                    })
                    .catch((error) => {
                        console.log('error')
                    });
                    
                }
            } 

        }) 
      }
        count++;
        timer=setTimeout(() => {
            getYuYuelink(count);
        }, 5000);
       
    })
}

getYuYuelink(1);
